package com.cat.model.biz.service.admin.impl;

import java.io.File;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alonew.core.commons.tuple.TwoTuple;
import com.alonew.db.dao.TxCommonDAO;
import com.cat.model.biz.service.admin.PhotographerInfoService;
import com.cat.model.common.model.common.PhotographerScalePictureVO;
import com.cat.model.common.model.common.PhotographerZedCardVO;
import com.cat.model.common.model.common.PhotographerInfoVO;
import com.cat.model.common.model.common.PhotographerStyleVO;
import com.cat.model.common.util.FileUtil;
import com.cat.model.common.util.StaticFileUtil;
import com.cat.model.dao.mapper.ModelMapper;

@Service("photographerInfoService")
public class PhotographerInfoServiceImpl implements PhotographerInfoService{
	@Autowired
	private TxCommonDAO commonDAO;
	
	@Autowired
	private ModelMapper modelMapper;
	
	@Override
	public PhotographerInfoVO createPhotographerInfo(PhotographerInfoVO photographerInfo,String[] style, File image,
			String imageFileName){
		if(image != null){
			String imagePath = StaticFileUtil.photographerInfoRelativePath() + FileUtil.buildDatePath();
			TwoTuple<String, byte[]> md5AndData = FileUtil.readAndCalMd5(image);
			FileUtil.writeFile(StaticFileUtil.staticRootPath() + imagePath, imageFileName, md5AndData.second);
			photographerInfo.setImage(imagePath + "/" + imageFileName);
		}
		commonDAO.insert(photographerInfo);
		
		if(style!=null&&style.length>0){
			for (int i = 0; i < style.length; i++) {   
				PhotographerStyleVO styleVO = new PhotographerStyleVO();
				styleVO.setPhotographerId(photographerInfo.getId());
				styleVO.setStyleCode(style[i]);
				commonDAO.insert(styleVO);
			}
		}
		
		return photographerInfo;
	}
	
	@Override
	public int deletePhotographerInfo(long id){
		PhotographerInfoVO photographerInfo = new PhotographerInfoVO();
		photographerInfo.setId(id);
		return commonDAO.deleteById(photographerInfo);
	}
	
	@Override
	public int updatePhotographerInfo(PhotographerInfoVO photographerInfo,String[] style, File image,
			String imageFileName){
		if(image != null){
			String imagePath = StaticFileUtil.photographerInfoRelativePath() + FileUtil.buildDatePath();
			TwoTuple<String, byte[]> md5AndData = FileUtil.readAndCalMd5(image);
			FileUtil.writeFile(StaticFileUtil.staticRootPath() + imagePath, imageFileName, md5AndData.second);
			photographerInfo.setImage(imagePath + "/" + imageFileName);
		}
		
		List<PhotographerStyleVO> styles = modelMapper.getPhotographerStyleList(photographerInfo.getId());
		if(styles!=null&&styles.size()>0){
			for (int i = 0; i < styles.size(); i++) {   
				PhotographerStyleVO styleVO = (PhotographerStyleVO)styles.get(i);
				if(null!=styleVO){
					commonDAO.deleteById(styleVO);
				}
			}
		}
		
		if(style!=null&&style.length>0){
			for (int i = 0; i < style.length; i++) {   
				PhotographerStyleVO styleVO = new PhotographerStyleVO();
				styleVO.setPhotographerId(photographerInfo.getId());
				styleVO.setStyleCode(style[i]);
				commonDAO.insert(styleVO);
			}
		}
		
		return commonDAO.updateById(photographerInfo, "name","price","cityCode","image","phoneNo","richResume","remark");
	}
	
	@Override
	public PhotographerScalePictureVO addPhotographerScalePic(PhotographerScalePictureVO vo,
			File image, String imageFileName)
			throws SQLException {
		if(image != null){
			String imagePath = StaticFileUtil.photographerInfoRelativePath() + FileUtil.buildDatePath();
			TwoTuple<String, byte[]> md5AndData = FileUtil.readAndCalMd5(image);
			FileUtil.writeFile(StaticFileUtil.staticRootPath() + imagePath, imageFileName, md5AndData.second);
			vo.setImage(imagePath + "/" + imageFileName);
		}
		
		commonDAO.insert(vo);
		return vo;
	}
	
	@Override
	public int deletePhotographerScalePic(long id){
		PhotographerScalePictureVO vo = new PhotographerScalePictureVO();
		vo.setId(id);
		return commonDAO.deleteById(vo);
	}

	@Override
	public int updatePhotographerScalePic(PhotographerScalePictureVO vo,
			File image, String imageFileName)
			throws SQLException {
		if(image != null){
			String imagePath = StaticFileUtil.photographerInfoRelativePath() + FileUtil.buildDatePath();
			TwoTuple<String, byte[]> md5AndData = FileUtil.readAndCalMd5(image);
			FileUtil.writeFile(StaticFileUtil.staticRootPath() + imagePath, imageFileName, md5AndData.second);
			vo.setImage(imagePath + "/" + imageFileName);
		}
		
		return commonDAO.updateById(vo, "image","mateType");
	}
	
	@Override
	public PhotographerZedCardVO addPhotographerZedCard(PhotographerZedCardVO vo,File image, String imageFileName)
			throws SQLException {
		if(image != null){
			String imagePath = StaticFileUtil.photographerInfoRelativePath() + FileUtil.buildDatePath();
			TwoTuple<String, byte[]> md5AndData = FileUtil.readAndCalMd5(image);
			FileUtil.writeFile(StaticFileUtil.staticRootPath() + imagePath, imageFileName, md5AndData.second);
			vo.setImage(imagePath + "/" + imageFileName);
		}
		
		commonDAO.insert(vo);
		return vo;
	}
	
	@Override
	public int deletePhotographerZedCard(long id){
		PhotographerZedCardVO vo = new PhotographerZedCardVO();
		vo.setId(id);
		return commonDAO.deleteById(vo);
	}

	@Override
	public int updatePhotographerZedCard(PhotographerZedCardVO vo,File image, String imageFileName)
			throws SQLException {
		if(image != null){
			String imagePath = StaticFileUtil.photographerInfoRelativePath() + FileUtil.buildDatePath();
			TwoTuple<String, byte[]> md5AndData = FileUtil.readAndCalMd5(image);
			FileUtil.writeFile(StaticFileUtil.staticRootPath() + imagePath, imageFileName, md5AndData.second);
			vo.setImage(imagePath + "/" + imageFileName);
		}
		
		return commonDAO.updateById(vo, "image");
	}
}
